import { Inject } from "@nestjs/common";
import { REQUEST } from "@nestjs/core";
import { MongooseModuleOptions, MongooseOptionsFactory } from "@nestjs/mongoose";

export class MongooseConfigService implements MongooseOptionsFactory {
    constructor(@Inject(REQUEST) private readonly request: Request) {
        
    }
    createMongooseOptions(): Promise<MongooseModuleOptions> | MongooseModuleOptions {
        const headers = this.request.headers
        const tenantId = headers['x-tenant-id'] || 'default'

        let url
        const defaultUrl = 'mongodb://root:example@localhost:27017/nest'
        if(tenantId == 'default') {
            url = defaultUrl
        } else {
            url = 'mongodb://root:example@localhost:27018/nest'
        }

        return {
            uri: url,
        } as MongooseModuleOptions
    }
}